package com.eedi.framework.template.controller.sys;

import com.eedi.framework.common.enums.YesOrNoEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.template.controller.sys.vo.menu.SysTemplateMenuListReq;
import com.eedi.framework.template.controller.sys.vo.menu.SysTemplateMenuSimpleResp;
import com.eedi.framework.template.dal.dataobject.SysTemplateMenuDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eedi.framework.template.controller.sys.vo.menu.SysTemplateMenuSaveReq;
import com.eedi.framework.template.controller.sys.vo.menu.SysTemplateMenuResp;
import com.eedi.framework.template.service.SysTemplatePermissionService;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import com.eedi.framework.template.convert.SysTemplateMenuConvert;

import static com.eedi.framework.common.pojo.CommonResult.success;

/**
 * @author zybourn
 */
@Tag(name = "系统界面 - 机构菜单模版")
@RestController
@RequestMapping("/temp-menu")
@Validated
public class SysTemplateMenuController {

    @Resource
    private SysTemplatePermissionService sysTemplatePermissionService;

    @PostMapping("/create")
    @Operation(summary = "创建菜单")
    @PreAuthorize("@sys.hasPermission('sys:temp-menu:create')")
    public CommonResult<String> createMenu(@Valid @RequestBody SysTemplateMenuSaveReq createReqVO) {
        createReqVO.setSysMenuVisible(YesOrNoEnum.YES);
        createReqVO.setSysMenuAlwaysShow(YesOrNoEnum.YES);
        createReqVO.setSysMenuKeepAlive(YesOrNoEnum.YES);
        String menuId = sysTemplatePermissionService.createMenu(createReqVO);
        return success(menuId);
    }

    @PutMapping("/update")
    @Operation(summary = "修改菜单")
    @PreAuthorize("@sys.hasPermission('sys:temp-menu:update')")
    public CommonResult<Boolean> updateMenu(@Valid @RequestBody SysTemplateMenuSaveReq updateReqVO) {
        sysTemplatePermissionService.updateMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除菜单")
    @Parameter(name = "sysTemplateMenuId", description = "菜单编号", required= true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:temp-menu:delete')")
    public CommonResult<Boolean> deleteMenu(@RequestParam("sysTemplateMenuId") String sysMenuId) {
        sysTemplatePermissionService.deleteMenu(sysMenuId);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取菜单列表", description = "用于【菜单管理】界面")
    @PreAuthorize("@sys.hasPermission('sys:temp-menu:query')")
    public CommonResult<List<SysTemplateMenuResp>> getMenuList(SysTemplateMenuListReq reqVO) {
        List<SysTemplateMenuDO> list = sysTemplatePermissionService.getMenuList(reqVO);
        list.sort(Comparator.comparing(SysTemplateMenuDO::getSysMenuSort));
        List<SysTemplateMenuResp> bean = BeanUtils.toBean(list, SysTemplateMenuResp.class);
        return success(SysTemplateMenuConvert.INSTANCE.buildMenuTree(bean));
    }

    @GetMapping({"/list-all-simple"})
    @Operation(summary = "获取菜单精简信息列表", description = "只包含被开启的菜单，用于【角色分配菜单】功能的选项。" +
            "在多租户的场景下，会只返回租户所在套餐有的菜单")
    public CommonResult<List<SysTemplateMenuSimpleResp>> getSimpleMenuList() {
        List<SysTemplateMenuDO> list = sysTemplatePermissionService.getMenuListByTenant(
//                new SysMenuListReq().setSysRoleStatus(CommonStatusEnum.ENABLE)
                new SysTemplateMenuListReq()
        );
        list.sort(Comparator.comparing(SysTemplateMenuDO::getSysMenuSort));
        List<SysTemplateMenuSimpleResp> bean = BeanUtils.toBean(list, SysTemplateMenuSimpleResp.class);
        return success(SysTemplateMenuConvert.INSTANCE.buildMenuSimpleTree(bean));
    }

    @GetMapping("/get")
    @Operation(summary = "获取菜单信息")
    @PreAuthorize("@sys.hasPermission('sys:temp-menu:query')")
    public CommonResult<SysTemplateMenuResp> getMenu(String sysTemplateMenuId) {
        SysTemplateMenuDO menu = sysTemplatePermissionService.getMenu(sysTemplateMenuId);
        return success(BeanUtils.toBean(menu, SysTemplateMenuResp.class));
    }

}
